package com.hyg.lib_music.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_music.R;
import com.hyg.lib_music.modle.FavMusicListData;
import com.hyg.lib_music.ui.activity.MusicMainActivity;

import java.util.List;

/**
 * @author: Administrator
 * @date: 2021/4/12
 */
public class MusicFavListAdapter extends RecyclerView.Adapter<MusicFavListAdapter.ViewHolder> {
    Context context;
    List<Music> list;

    public MusicFavListAdapter(Context context, List<Music> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_fav_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                list.get(position).isChecked = isChecked;
            }
        });

        if (list.get(position).isEditable) {
            holder.checkBox.setVisibility(View.VISIBLE);
            holder.checkBox.setChecked(list.get(position).isChecked);
        } else {
            holder.checkBox.setVisibility(View.GONE);
        }
        Music music = MusicMainActivity.serviceBinder.getCurrentMusic();
        if (music != null && music.musicName.equals(list.get(position).musicName)) {
            holder.playing_image.setVisibility(View.VISIBLE);
            holder.name.setTextColor(context.getResources().getColor(R.color.mainMusicColor));
        } else {
            holder.playing_image.setVisibility(View.GONE);
            holder.name.setTextColor(context.getResources().getColor(R.color.musicTextBlackDeep));
        }
        String author = list.get(position).musician;
        if (TextUtils.isEmpty(author) || author.equals("无")) {
            author = "";
        } else {
            author = "-" + author;
        }
        holder.name.setText(list.get(position).musicName + author);

        holder.ln_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MusicMainActivity.serviceBinder.addPlayList(list.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox;
        TextView name;
        ImageView playing_image;
        LinearLayout ln_root;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.iv_collection);
            name = itemView.findViewById(R.id.tv_name);
            playing_image = itemView.findViewById(R.id.iv_playing_hint);
            ln_root = itemView.findViewById(R.id.ln_root);
        }
    }

}
